"""
Player tank implementation
"""
import pygame
from typing import List, Optional
from .tank import Tank
from utils.constants import Direction, TankType
from utils.config import PLAYER_LIVES


class PlayerTank(Tank):
    """Player-controlled tank"""
    
    def __init__(self, x: int, y: int):
        """
        Initialize player tank
        
        Args:
            x: X coordinate
            y: Y coordinate
        """
        super().__init__(x, y, TankType.PLAYER)
        
        # Player-specific properties
        self.lives = PLAYER_LIVES
        self.score = 0
        self.bullets_fired = 0
        
        # Input handling
        self.keys_pressed = set()
        self.last_shot_time = 0
        self.shot_cooldown = 500  # milliseconds
        
    def handle_input(self, keys: List[bool], current_time: int):
        """
        Handle player input
        
        Args:
            keys: List of pressed keys
            current_time: Current time in milliseconds
        """
        # Movement keys
        if keys[pygame.K_w] or keys[pygame.K_UP]:
            self.move(Direction.UP)
        elif keys[pygame.K_s] or keys[pygame.K_DOWN]:
            self.move(Direction.DOWN)
        elif keys[pygame.K_a] or keys[pygame.K_LEFT]:
            self.move(Direction.LEFT)
        elif keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            self.move(Direction.RIGHT)
        else:
            self.moving = False
        
        # Shooting
        if keys[pygame.K_SPACE] and self._can_shoot(current_time):
            self.last_shot_time = current_time
            return True  # Signal that bullet should be fired
        
        return False
    
    def _can_shoot(self, current_time: int) -> bool:
        """Check if tank can shoot"""
        return current_time - self.last_shot_time >= self.shot_cooldown
    
    def add_score(self, points: int):
        """Add points to player score"""
        self.score += points
    
    def get_lives(self) -> int:
        """Get remaining lives"""
        return self.lives
    
    def get_score(self) -> int:
        """Get current score"""
        return self.score
    
    def _on_destroyed(self):
        """Called when player tank is destroyed"""
        # Player-specific destruction behavior
        self.lives -= 1
        if self.lives > 0:
            # Respawn player
            self._respawn_player()
        else:
            # Game over
            pass
    
    def _respawn_player(self):
        """Respawn player tank"""
        # This will be called by the game when respawning
        pass
    
    def respawn(self, x: int, y: int):
        """
        Respawn player tank
        
        Args:
            x: X coordinate
            y: Y coordinate
        """
        self.x = x
        self.y = y
        self.rect.x = x
        self.rect.y = y
        self.direction = Direction.UP
        self.moving = False
        self.last_shot_time = 0
        # Don't reset lives here - they're managed by the game
    
    def render(self, screen: pygame.Surface):
        """Render player tank with special effects"""
        if not self.is_alive():
            return
        
        # Call parent render method
        super().render(screen)
        
        # Add player-specific visual effects
        self._render_player_effects(screen)
    
    def _render_player_effects(self, screen: pygame.Surface):
        """Render player-specific visual effects"""
        # Draw life indicator
        if self.lives > 1:
            font = pygame.font.Font(None, 12)
            lives_text = font.render(f"Lives: {self.lives}", True, (255, 255, 255))
            screen.blit(lives_text, (self.x, self.y - 15))
        
        # Draw score
        font = pygame.font.Font(None, 12)
        score_text = font.render(f"Score: {self.score}", True, (255, 255, 255))
        screen.blit(score_text, (self.x, self.y - 30))
        
        # Draw shooting cooldown indicator
        if not self._can_shoot(pygame.time.get_ticks()):
            # Draw a small red dot to indicate cooldown
            pygame.draw.circle(screen, (255, 0, 0), 
                             (self.x + self.width - 5, self.y + 5), 3)